package com.cxm.helper.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.cxm.helper.enums.LikedStatusEnum;
import com.cxm.helper.exception.BusinessException;
import com.cxm.helper.pojo.Article;
import com.cxm.helper.pojo.dto.ArticleAddDTO;
import com.cxm.helper.pojo.dto.ArticleListDTO;
import com.cxm.helper.pojo.dto.UserLikeDTO;
import com.cxm.helper.pojo.vo.ArticleVO;
import com.cxm.helper.service.ArticleService;
import com.cxm.helper.service.UserLikeService;
import com.cxm.helper.service.impl.AliyunOssService;
import com.cxm.helper.util.ResponseEntity;
import com.cxm.helper.util.constant.AliyunOssConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author: chenximeng
 * @date: 2022/11/22 17:00
 */
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;
    @Autowired
    UserLikeService userLikeService;
    @Autowired
    AliyunOssService aliyunOssService;

    @PostMapping("/list")
    public ResponseEntity list(@RequestBody ArticleListDTO articleListDTO){
        return ResponseEntity.ok(articleService.listArticle(articleListDTO));
    }

    @GetMapping("/getById/{id}")
    public ResponseEntity<ArticleVO> getById(@PathVariable Long id) {
        return ResponseEntity.ok(articleService.getVoById(id));
    }

    @GetMapping("/del/{id}")
    public ResponseEntity del(@PathVariable Long id) {
        if (id == null) {
            throw new BusinessException();
        }
        articleService.removeById(id);
        return ResponseEntity.ok();
    }

    @PostMapping("/add")
    public ResponseEntity add(@RequestBody ArticleAddDTO articleAddDTO) {
        Article article = new Article();
        BeanUtils.copyProperties(articleAddDTO, article);
        article.setCreateUser(StpUtil.getLoginIdAsLong());
        articleService.save(article);
        return ResponseEntity.ok();
    }

    @PostMapping("/uploadImg")
    public ResponseEntity<String> uploadUserAvatar(MultipartFile file) {
        //返回图片在oss上的路径
        String url;
        try{
            url = aliyunOssService.uploadFileAvatar(file, AliyunOssConstant.ARTICLE_AVATAR_PATH);
        }catch (Exception e) {
            log.error("文件上传报错",e);
            throw new BusinessException("上传文件失败");
        }
        ResponseEntity<String> ok = ResponseEntity.ok();
        return ok.setData(url);
    }

    @PostMapping("/like")
    public ResponseEntity like(@RequestBody UserLikeDTO userLikeDTO){
        Long loginUserId = StpUtil.getLoginIdAsLong();
        if (userLikeDTO.getLikeStatus().equals(LikedStatusEnum.LIKE.getCode())) {
            userLikeService.saveLiked2Redis(loginUserId, userLikeDTO.getArticleId());
        }else {
            userLikeService.unlikeFromRedis(loginUserId, userLikeDTO.getArticleId());
        }
        return ResponseEntity.ok();
    }
}
